package org.xml.xpath4js.client;

/* 
xpath4js is a GWT implementation of XPath 2.0 
Copyright (C) 2010  Peter Schonefeld (peter dot schonefeld at gmail)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.Vector;

@SuppressWarnings("unchecked")
public class XPathStaticEnv {

	//all types available to the expression;
	/**
     * @gwt.typeArgs <com.artbase.xpath.client.KeyTypePair>
     */		
	Vector Types = new Vector(); //GWT Compile

	//See all variable info in dynamic environment

	//allows multiple function of the same name
	//differing only in the number and type
	//of the arguments.
	/**
     * @gwt.typeArgs <com.artbase.xpath.client.NameFuncSignaturePair>
     */			
	Vector Functions = new Vector();  //GWT Compile
	
	public XPathStaticEnv(){
		this.InitTypes();
		this.InitFunctions();	
	};

	public void InitTypes(){
		this.Types = new Vector();
		this.Types.add(new KeyTypePair("xs:anySimpleType", new XSDType("xs","anySimpleType")));
		this.Types.add(new KeyTypePair( "xs:anyType", new XSDType("xs", "anyType")));
		this.Types.add(new KeyTypePair( "xs:string", new XSDType("xs", "string")));
		this.Types.add(new KeyTypePair( "xs:integer", new XSDType("xs", "integer")));
		this.Types.add(new KeyTypePair( "xs:decimal", new XSDType("xs", "decimal")));
		this.Types.add(new KeyTypePair( "xs:double", new XSDType("xs", "double")));
		this.Types.add(new KeyTypePair( "dm:sequence", new XSDType("dm", "sequence")));
		this.Types.add(new KeyTypePair( "dm:item", new XSDType("dm", "item")));
		this.Types.add(new KeyTypePair( "dm:node", new XSDType("dm", "node")));		
	};
	
	public void InitFunctions(){ //TODO:
		//XSDType item = ((KeyTypePair)this.Types.get(7)).Type; //TODO: make map 
		//XSDType node = ((KeyTypePair)this.Types.get(8)).Type;
		//XSDType integer = ((KeyTypePair)this.Types.get(3)).Type;
/*		
		//**function -- fn:root
		this.Functions["root"] = new XPathFuncSigs();
		map<string,XPathFuncSigs*>::iterator it = this->Functions.find("root");
		//fn:root() as node
		it->second->Signatures.push_back(new XPathFunction("fn","root",ptNode));
		//fn:root($srcval as node) as node
		it->second->Signatures.push_back(new XPathFunction("fn","root",ptNode));
		it->second->Signatures.back()->Arity.push_back(ptNode);

		//**function -- fn:position
		XPathFuncSigs *pFunc = this->Functions["position"] = new XPathFuncSigs();
//		map<string,XPathFuncSigs*>::iterator it = this->Functions.find("position");
		//fn:position() as unsignedInt?
		pFunc->Signatures.push_back(new XPathFunction("fn","position",ptInt));
		pFunc->Signatures.back()->PFunc = &CEvalEngine::FuncPosition;
				*/
	};

	public XPathFunction GetFunction(String ns, String name, XPathFuncArity arity){
		/* TODO:
		//find the set of functions matching the local name.
		map<string,XPathFuncSigs*>::iterator it = this->Functions.find(name);
		//if found iterate thru the function signatures until a match is found and return
		if(it!=this->Functions.end()){	
			vector<XPathFunction*>::iterator jt = it->second->Signatures.begin();
			for(jt;jt!=it->second->Signatures.end();jt++){
				if((*jt)->Namespace==ns){
					if( ((int) (*jt)->Arity.size()==0 && !arity) || 
						((*jt)->Arity == (*arity)) )
						return (*jt);
				}
			}
		}
		return NULL;
		*/
		return null;
	};
}
